#include <stdio.h>
#include <stdlib.h>

int main (int argc, char const* argv[])
{
    int i, a, b, n, ans, first, second;

    while (scanf("%d %d %d", &a, &b, &n) != EOF) { 
        if (0 == a && 0 == b && 0 == n) break;

        for (i = 1; i <= n; i++) {
            if (1 == i || 2 == i) {
                ans = 1;
                first = 1; second = 1;
            } else {
                ans = (a * second + b * first) % 7;
                first = second;
                second = ans;
            }
        }

        printf("%d\n", ans);
    }

    return 0;
}
